package org.jeecg.modules.ywPalletEnter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.constant.CommonConstant;
import org.jeecg.constant.InventoryConstant;
import org.jeecg.modules.ywPalletEnter.entity.YwPalletEnterDetail;
import org.jeecg.modules.ywPalletEnter.entity.YwPalletEnterMain;
import org.jeecg.modules.ywPalletEnter.mapper.YwPalletEnterDetailMapper;
import org.jeecg.modules.ywPalletEnter.mapper.YwPalletEnterMainMapper;
import org.jeecg.modules.ywPalletEnter.service.IYwPalletEnterDetailService;
import org.jeecg.modules.ywPalletEnter.service.IYwPalletEnterMainService;
import org.jeecg.modules.ywPalletInventory.entity.YwPalletInventoryInfo;
import org.jeecg.modules.ywPalletInventory.service.IYwPalletInventoryInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 托盘入库
 * @Author: jeecg-boot
 * @Date: 2024-08-27
 * @Version: V1.0
 */
@Service
public class YwPalletEnterMainServiceImpl extends ServiceImpl<YwPalletEnterMainMapper, YwPalletEnterMain> implements IYwPalletEnterMainService {

    @Resource
    private YwPalletEnterMainMapper ywPalletEnterMainMapper;
    @Resource
    private YwPalletEnterDetailMapper ywPalletEnterDetailMapper;
    @Resource
    private IYwPalletEnterDetailService ywPalletEnterDetailService;
    @Resource
    private IYwPalletInventoryInfoService ywPalletInventoryInfoService;

    @Override
    @Transactional
    public void saveMain(YwPalletEnterMain ywPalletEnterMain, List<YwPalletEnterDetail> ywPalletEnterDetailList) {
        Assert.notEmpty(ywPalletEnterDetailList, "入库单数据异常,请填写入库单详情！");
        ywPalletEnterMain.setEnterStatus(InventoryConstant.EnterStatusEnum.CREATE_END.getCode());
        ywPalletEnterMainMapper.insert(ywPalletEnterMain);

        for (YwPalletEnterDetail entity : ywPalletEnterDetailList) {
            entity.setEnterId(ywPalletEnterMain.getId());
            entity.setEnterVolume(entity.getPalletVolume().multiply(BigDecimal.valueOf(entity.getEnterNum())));
            entity.setStatus(CommonConstant.StatusEnum.YES.getCode());
            ywPalletEnterDetailMapper.insert(entity);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> confirmEnter(String id) {
        YwPalletEnterMain enterMain = this.ywPalletEnterMainMapper.selectById(id);
        Assert.notNull(enterMain, "入库单数据异常,请联系管理员！");
        Assert.isFalse(InventoryConstant.EnterStatusEnum.DELETE.getCode().equals(enterMain.getEnterStatus()), "入库单状态异常,请联系管理员！");
        List<YwPalletEnterDetail> enterDetailList = ywPalletEnterDetailService.selectList(id);
        Assert.notEmpty(enterDetailList, "入库单数据异常,请联系管理员！");

        long errorDataCount = enterDetailList.stream().filter(data -> data.getEnterNum() < 0).count();
        Assert.isTrue(errorDataCount == 0, "入库重量不能小于0！");

        // 更新主表主库状态
        enterMain.setEnterStatus(InventoryConstant.EnterStatusEnum.ENTER_END.getCode());
        this.ywPalletEnterMainMapper.updateById(enterMain);

        // 封装库存信息
        for (YwPalletEnterDetail enterDetail : enterDetailList) {
            // 查询托盘部门的库存
            YwPalletInventoryInfo palletInventoryInfo = ywPalletInventoryInfoService.getOne(
                    new QueryWrapper<YwPalletInventoryInfo>()
                            .eq("pallet_dept", enterDetail.getPalletDept()).eq("status", CommonConstant.StatusEnum.YES.getCode()));
            // 托盘部门在库存中不存在：新增；存在：增加
            if (null == palletInventoryInfo) {
                palletInventoryInfo = new YwPalletInventoryInfo();
                palletInventoryInfo.setPalletDept(enterDetail.getPalletDept());
                palletInventoryInfo.setPalletVolume(enterDetail.getPalletVolume());
                palletInventoryInfo.setInventoryNum(enterDetail.getEnterNum());
                palletInventoryInfo.setInventoryVolume(enterDetail.getPalletVolume().multiply(BigDecimal.valueOf(enterDetail.getEnterNum())));
                palletInventoryInfo.setStatus(CommonConstant.StatusEnum.YES.getCode());
                ywPalletInventoryInfoService.save(palletInventoryInfo);
            } else {
                palletInventoryInfo.setInventoryNum(palletInventoryInfo.getInventoryNum() + enterDetail.getEnterNum());
                palletInventoryInfo.setInventoryVolume(palletInventoryInfo.getInventoryVolume().add(enterDetail.getEnterVolume()));
                ywPalletInventoryInfoService.updateById(palletInventoryInfo);
            }
        }

        return Result.OK("入库成功！");
    }

    @Override
    @Transactional
    public void updateMain(YwPalletEnterMain ywPalletEnterMain, List<YwPalletEnterDetail> ywPalletEnterDetailList) {
        Assert.notEmpty(ywPalletEnterDetailList, "入库单数据异常,请填写入库单详情！");
        ywPalletEnterMainMapper.updateById(ywPalletEnterMain);

        ywPalletEnterDetailMapper.deleteByMainId(ywPalletEnterMain.getId());

        for (YwPalletEnterDetail entity : ywPalletEnterDetailList) {
            entity.setId(null);
            entity.setEnterId(ywPalletEnterMain.getId());
            entity.setEnterVolume(entity.getPalletVolume().multiply(BigDecimal.valueOf(entity.getEnterNum())));
            entity.setStatus(CommonConstant.StatusEnum.YES.getCode());
            entity.setCreateTime(null);
            entity.setCreateBy(null);
            ywPalletEnterDetailMapper.insert(entity);
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        // 更新子表有效标志
        ywPalletEnterDetailMapper.deleteByMainId(id);

        // 更新主表有效标志
        YwPalletEnterMain enterMain = new YwPalletEnterMain();
        enterMain.setId(id);
        enterMain.setEnterStatus(InventoryConstant.EnterStatusEnum.DELETE.getCode());
        ywPalletEnterMainMapper.updateById(enterMain);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            this.delMain(id.toString());
        }
    }

}
